package com.example.meitao.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.meitao.R;
import com.example.meitao.adapter.MySayAdapter;
import com.example.meitao.bean.Say;
import com.example.meitao.config.NetConfig;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 琚雷 on 2016-9-2.
 */
public class SayFragment extends Fragment {

   private RecyclerView mRecycleView;
    private List<Say.DataBean.ResultsBean> mData;
    private MySayAdapter  madapter;
    private LinearLayoutManager mLayoutManager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =LayoutInflater.from(getActivity()).inflate(R.layout.fragment_say,null);
        initView(view);
        ivitData();
        setData();
        return  view;
    }

    private void setData() {
        //设置setadapter
        mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mRecycleView.setLayoutManager(mLayoutManager);
        mRecycleView.setAdapter(madapter);


        x.http().get(new RequestParams(NetConfig.PATH_SAY), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson =new Gson();
                Say say = gson.fromJson(result, Say.class);
                mData.addAll(say.getData().getResults());
                 madapter.notifyDataSetChanged();

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(getActivity(), "网络连接失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void ivitData() {
        mData =new ArrayList<>();
     madapter =new MySayAdapter(mData,getActivity());
    }

    private void initView(View view) {
        mRecycleView = (RecyclerView) view.findViewById(R.id.rv_say_test);

    }

}
